Component({
  mixins: [],
  data: {
    searchInputKey:'',
    userOptionShow:false,
    notLogin:true,
    userInfo:null,
    receiveLogin:false
  },
  props: {
    flag:{
      type:Boolean
    },
    isLogin:{
      type:Boolean
    },
    giveKey:{
      type:String
    },
    onChange:()=>{} // 事件传递
  },
  didMount() {
   this.setData({
     receiveLogin:this.props.isLogin
   })
  },
  didUpdate() {
    if(this.data.receiveLogin!=this.props.isLogin){
      this.setData({
        receiveLogin:this.props.isLogin
      })
      this.getUserInfo();
    }
  },
  didUnmount() {
    this.setState = (state,callback)=>{
      return;
    };
  },
  methods: {
    bindSearchKey(e){
      this.setData({
        searchInputKey: e.detail.value,
      });
    },
    search(){
      if(this.props.flag){
        //在index页
        my.navigateTo({ url: '../../pages/search/search?key='+ this.data.searchInputKey })
      }else{
        //在search页
        this.even()
        // if(!this.data.searchInputKey){
        //   console.log(this.props.giveKey)
        // }else{
        //   this.even()
        // }
      }
    },
    bindOptionShow(){
      console.log(1111)
      this.setData({
        userOptionShow:!this.data.userOptionShow
      })
    },
    getUserInfo(){
      //判断缓存里面有没有userInfo，没有的话,notLogin还是=true
      //有的话，notLogin=false；缓存userInfo赋给userInfo
      let that = this;
      my.getStorage({
        key: 'userInfo',
        success: function(res) {
          if(res.data){
            that.setData({
              notLogin:false,
              userInfo:res.data.userInfo
            })
          }else{
            that.setData({
              notLogin:true,
              userInfo:null
            })
          }
        },
      });
    },
    bindExit(){
      //调退出的接口，并且清除缓存
      let that = this;
      my.removeStorage({
        key: 'orderInfo'
      });
      my.removeStorage({
        key: 'selectedAddress'
      });
      my.removeStorage({
        key: 'shareInfo'
      });
      my.removeStorage({
        key: 'userInfo',
        success: function(){
          that.setData({
            notLogin:true,
            userInfo:null
          })
          // 跳转到登陆页
          my.reLaunch({
            url: '../../pages/login/login'
          })
        }
      });
    },
    even() {
      // 给父组件传递数据
      this.props.onChange(this.data.searchInputKey)
    }
  },
});
